package fxbattle.client.model;

import java.util.HashMap;
import java.util.Map;

public final class Location {

	private static final Map<Integer, Map<Integer, Location>> locations = new HashMap<Integer, Map<Integer,Location>>();
	
	public static Location getLocation(int x, int y){
		if(locations.get(x) == null){
			locations.put(x, new HashMap<Integer, Location>());
		}
		if(locations.get(x).get(y) == null){
			locations.get(x).put(y, new Location(x, y));
		}
		return locations.get(x).get(y);
	}
	
	private final int x, y;
	
	private Location(int ix, int iy){
		x = ix;
		y = iy;
	}
	
	public int getX(){ return x; }
	public int getY(){ return y; }
	
	public boolean isEvenColumn(){
		return getY() % 2 == 0;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj.getClass() == null || obj.getClass() != Location.class){
			return false;
		}
		Location lObj = (Location)obj;
		return lObj.x == x && lObj.y == y;
	}
	
	@Override
	public int hashCode() {
		return x * 37 + y * 17;
	}
	
	@Override
	public String toString() {
		return "(" + x + ", " + y + ")";
	}
	
	public Direction getDirectionTo(Location iLocToHeadTo){
		for(Direction lDir: Direction.values()){
			if(lDir.goFrom(this) == iLocToHeadTo){
				return lDir;
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(Location.getLocation(0, 0).getDirectionTo(Location.getLocation(0, 1)));
	}
	
}
